// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.erosion.obj;

import java.util.ArrayList;
import zombie.erosion.ErosionMain;
import zombie.iso.IsoGridSquare;
import zombie.iso.IsoObject;
import zombie.iso.objects.IsoTree;
import zombie.iso.sprite.IsoSprite;
import zombie.iso.sprite.IsoSpriteManager;
import zombie.util.list.PZArrayList;

public final class ErosionObj {
    private final ErosionObjSprites sprites;
    public String name;
    public int stages;
    public boolean hasSnow;
    public boolean hasFlower;
    public boolean hasChildSprite;
    public float bloomStart;
    public float bloomEnd;
    public boolean noSeasonBase;
    public int cycleTime = 1;

    public ErosionObj(ErosionObjSprites _sprites, int _cycleTime, float _bloomstart, float _bloomend, boolean _noSeasonBase) {
        this.sprites = _sprites;
        this.name = _sprites.name;
        this.stages = _sprites.stages;
        this.hasSnow = _sprites.hasSnow;
        this.hasFlower = _sprites.hasFlower;
        this.hasChildSprite = _sprites.hasChildSprite;
        this.bloomStart = _bloomstart;
        this.bloomEnd = _bloomend;
        this.noSeasonBase = _noSeasonBase;
        this.cycleTime = _cycleTime;
    }

    public IsoObject getObject(IsoGridSquare _sq, boolean _bRemove) {
        PZArrayList pZArrayList = _sq.getObjects();

        for (int _int = pZArrayList.size() - 1; _int >= 0; _int--) {
            IsoObject isoObject = (IsoObject)pZArrayList.get(_int);
            if (this.name.equals(isoObject.getName())) {
                if (_bRemove) {
                    pZArrayList.remove(_int);
                }

                isoObject.doNotSync = true;
                return isoObject;
            }
        }

        return null;
    }

    public IsoObject createObject(IsoGridSquare _sq, int _stage, boolean _bTree, int _season) {
        String string = this.sprites.getBase(_stage, this.noSeasonBase ? 0 : _season);
        if (string == null) {
            string = "";
        }

        Object object;
        if (_bTree) {
            object = IsoTree.getNew();
            ((IsoObject)object).sprite = (IsoSprite)IsoSpriteManager.instance.NamedMap.get(string);
            ((IsoObject)object).square = _sq;
            ((IsoObject)object).sx = 0.0F;
            ((IsoTree)object).initTree();
        } else {
            object = IsoObject.getNew(_sq, string, this.name, false);
        }

        ((IsoObject)object).setName(this.name);
        ((IsoObject)object).doNotSync = true;
        return (IsoObject)object;
    }

    public boolean placeObject(IsoGridSquare _sq, int _stage, boolean _bTree, int _season, boolean _bloom) {
        IsoObject isoObject = this.createObject(_sq, _stage, _bTree, _season);
        if (isoObject != null && this.setStageObject(_stage, isoObject, _season, _bloom)) {
            isoObject.doNotSync = true;
            if (!_bTree) {
                _sq.getObjects().add(isoObject);
                isoObject.addToWorld();
            } else {
                _sq.AddTileObject(isoObject);
            }

            return true;
        } else {
            return false;
        }
    }

    public boolean setStageObject(int _stage, IsoObject _object, int _season, boolean _bloom) {
        _object.doNotSync = true;
        if (_stage >= 0 && _stage < this.stages && _object != null) {
            String string = this.sprites.getBase(_stage, this.noSeasonBase ? 0 : _season);
            if (string == null) {
                _object.setSprite(this.getSprite(""));
                if (_object.AttachedAnimSprite != null) {
                    _object.AttachedAnimSprite.clear();
                }

                return true;
            } else {
                IsoSprite isoSprite = this.getSprite(string);
                _object.setSprite(isoSprite);
                if (this.hasChildSprite || this.hasFlower) {
                    if (_object.AttachedAnimSprite == null) {
                        _object.AttachedAnimSprite = new ArrayList();
                    }

                    _object.AttachedAnimSprite.clear();
                    if (this.hasChildSprite && _season != 0) {
                        string = this.sprites.getChildSprite(_stage, _season);
                        if (string != null) {
                            isoSprite = this.getSprite(string);
                            _object.AttachedAnimSprite.add(isoSprite.newInstance());
                        }
                    }

                    if (this.hasFlower && _bloom) {
                        string = this.sprites.getFlower(_stage);
                        if (string != null) {
                            isoSprite = this.getSprite(string);
                            _object.AttachedAnimSprite.add(isoSprite.newInstance());
                        }
                    }
                }

                return true;
            }
        } else {
            return false;
        }
    }

    public boolean setStage(IsoGridSquare _sq, int _stage, int _season, boolean _bloom) {
        IsoObject isoObject = this.getObject(_sq, false);
        return isoObject != null ? this.setStageObject(_stage, isoObject, _season, _bloom) : false;
    }

    public IsoObject removeObject(IsoGridSquare _sq) {
        return this.getObject(_sq, true);
    }

    private IsoSprite getSprite(String string) {
        return ErosionMain.getInstance().getSpriteManager().getSprite(string);
    }
}
